import axios from 'axios'

// 创建 axios 实例
const api = axios.create({
  baseURL: '/api', // 你可以根据实际 API 路径调整
  timeout: 10000, // 设置请求超时
  headers: {
    'Content-Type': 'application/json',
  },
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    // 如果需要，可以在这里加入 Token 等请求头
    // config.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}`;
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    return response.data // 返回响应数据
  },
  (error) => {
    if (error.response) {
      // 处理错误响应，比如提示信息
      console.error('API Error: ', error.response)
    }
    return Promise.reject(error)
  },
)

export default api

// 封装所有接口方法

// 1. 居民信息管理
export const submitResident = (residentData) => {
  return api.post('/residents', residentData)
}

export const getAllResidents = () => {
  return api.get('/residents')
}

export const getResidentById = (id) => {
  return api.get(`/residents/${id}`)
}

export const updateResident = (id, residentData) => {
  return api.put(`/residents/${id}`, residentData)
}

export const deleteResident = (id) => {
  return api.delete(`/residents/${id}`)
}

// 2. 房屋信息管理
export const submitHouse = (houseData) => {
  return api.post('/houses', houseData)
}

export const getAllHouses = () => {
  return api.get('/houses')
}

export const getHouseById = (id) => {
  return api.get(`/houses/${id}`)
}

export const updateHouse = (id, houseData) => {
  return api.put(`/houses/${id}`, houseData)
}

export const deleteHouse = (id) => {
  return api.delete(`/houses/${id}`)
}

// 3. 物业管理
export const submitProperty = (propertyData) => {
  return api.post('/properties', propertyData)
}

export const getAllProperties = () => {
  return api.get('/properties')
}

export const getPropertyById = (id) => {
  return api.get(`/properties/${id}`)
}

export const updateProperty = (id, propertyData) => {
  return api.put(`/properties/${id}`, propertyData)
}

export const deleteProperty = (id) => {
  return api.delete(`/properties/${id}`)
}

// 4. 社区活动管理
export const submitActivity = (activityData) => {
  return api.post('/activities', activityData)
}

export const getAllActivities = () => {
  return api.get('/activities')
}

export const getActivityById = (id) => {
  return api.get(`/activities/${id}`)
}

export const updateActivity = (id, activityData) => {
  return api.put(`/activities/${id}`, activityData)
}

export const deleteActivity = (id) => {
  return api.delete(`/activities/${id}`)
}

// 5. 投诉建议管理
export const submitComplaint = (complaintData) => {
  return api.post('/complaints', complaintData)
}

export const getAllComplaints = () => {
  return api.get('/complaints')
}

export const getComplaintById = (id) => {
  return api.get(`/complaints/${id}`)
}

export const updateComplaint = (id, complaintData) => {
  return api.put(`/complaints/${id}`, complaintData)
}

export const deleteComplaint = (id) => {
  return api.delete(`/complaints/${id}`)
}

// 6. 系统管理
export const submitSystemUser = (userData) => {
  return api.post('/system/users', userData)
}

export const getAllSystemUsers = () => {
  return api.get('/system/users')
}

export const getSystemUserById = (id) => {
  return api.get(`/system/users/${id}`)
}

export const updateSystemUser = (id, userData) => {
  return api.put(`/system/users/${id}`, userData)
}

export const deleteSystemUser = (id) => {
  return api.delete(`/system/users/${id}`)
}

export const register = async (username, password, identity) => {
  try {
    const response = await api.post('/api/register', { username, password, identity })
    return response.data
  } catch (error) {
    throw new Error('注册失败，请重试')
  }
}

export const login = async (username, password) => {
  try {
    const response = await api.post('/api/login', { username, password })
    return response.data
  } catch (error) {
    throw new Error('登录失败，请检查用户名和密码')
  }
}

export const addDevice = async (deviceData) => {
  return api.post('/devices', deviceData)
}
export const addFee = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const deleteDevice = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const deleteFee = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const getAllDevices = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const getAllFees = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const updateDevice = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const updateFee = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const addComplaint = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const addActivity = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const addUser = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const deleteUser = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const deletePermission = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const getPermissions = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const getUsers = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const updatePermission = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
export const updateUser = async (deviceData) => {
  return api.post('/addFee', deviceData)
}
